﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vibes.Common.Interfaces.Services;
using Enyim.Caching;
using Enyim.Caching.Configuration;

namespace GuitarVibes.BL.Services
{
    public class EnyimCachingService : ICachingService, IDisposable
    {
        private readonly MemcachedClient _client;

        public EnyimCachingService()
        {
            var conf = new MemcachedClientConfiguration();
            conf.AddServer("127.0.0.1:11211");
            conf.Protocol = Enyim.Caching.Memcached.MemcachedProtocol.Binary;
            conf.SocketPool.ConnectionTimeout = TimeSpan.FromSeconds(2);
            conf.SocketPool.DeadTimeout = TimeSpan.FromSeconds(4);
            conf.SocketPool.MaxPoolSize = 20;
            conf.SocketPool.ReceiveTimeout = TimeSpan.FromSeconds(2);
            _client = new MemcachedClient(conf);
        }

        public void Dispose()
        {
            _client.Dispose();
        }

        public T Get<T>(string key)
        {
            return _client.Get<T>(key);
        }
    }
}
